----------------------------------------------------------------------------------
-- Company: 
-- Engineer: Will Rozzo
-- 
-- Create Date:    19:53:38 07/07/2009 
-- Design Name: 
-- Module Name:    DataDirDriver - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity DataDirDriver is
 Port ( write_en : out std_logic;
		  read_en : out std_logic;
				clk : in  STD_LOGIC;
           reset : in  STD_LOGIC);
           --dataDirection : out  STD_LOGIC);
end DataDirDriver;

architecture Behavioral of DataDirDriver is
--added
type State is ( read_to_robot, read_to_externalDevice, write_data );
signal current_state, next_state : State;
--end added
begin


--added
state_update : process(clk,reset) is
	begin
		if( rising_edge(clk) ) then
			if( reset = '1' ) then
				current_state <= write_data;	
			else --update the current state when clock pulses high
				current_state <= next_state;
			end if;
		end if;
	end process state_update; 
		
	combinational : process(current_state) --activate if current state changes
		begin	 
			
			case current_state is
				when read_to_robot =>
						
						write_en <= '1'; 
						read_en <= '0';
						--dataDirection <= '1';  -- tell the RS232 that we want to push data onto the fifo buffer
						next_state <= write_data;
				
				when read_to_externalDevice =>
					--do nothing
					next_state <= write_data;
				
				when write_data =>
						
						read_en <= '1';
						write_en <= '0'; 
						--dataDirection <= '0';  -- tell the RS232 that we want to send data out to the robot
						next_state <= read_to_robot;  -- keep writing until told otherwise
				
			
				when others =>
					next_state <= write_data;
					
			end case;  
			
		end process combinational;

--end added

end Behavioral;

